package com.tencent.elife.view;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

/**
 * @Description: 按钮开关控件，支持两个或者多个按钮切换
 * @date 2012-5-31
 * @author zijianlu
 */
public class SwitchButton extends LinearLayout implements OnClickListener {

    public static final int NONE = -1;

    private Context mContext;
    private OnSwitchListener mOnSwitchListener;

    private int mCurIdx = NONE;
    private int mSelColor = Color.parseColor("#FFFFFF");
    private int mNormColor = Color.parseColor("#a3a3a3");

    private int mLeftBgResId;
    private int mMidBgResId;
    private int mRightResId;

    public SwitchButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    /** 设置文字选中和非选中状态的颜色 */
    public void setTextColor(int normTextColor, int selTextColor) {
        mNormColor = normTextColor;
        mSelColor = selTextColor;
        updateTextColor();
    }

    /** 设置按钮正常状态的背景资源图片, 3个按钮时 */
    public void setItemBackground(int leftBg, int midBg, int rightBg) {
        mLeftBgResId = leftBg;
        mMidBgResId = midBg;
        mRightResId = rightBg;

        updateBackGround();
    }

    /** 设置按钮文本 */
    public void setItemText(String[] txtArray) {
        if (txtArray == null || txtArray.length <= 0) {
            return;
        }

        for (int i = 0; i < txtArray.length; i++) {
            Button bt = new Button(mContext);
            LayoutParams leftLp = new LinearLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
            leftLp.weight = 1f;
            leftLp.gravity = Gravity.CENTER;
            bt.setLayoutParams(leftLp);
            bt.setText(txtArray[i]);
            bt.setTag(i);
            bt.setTextColor(mNormColor);

            bt.setOnClickListener(this);
            addView(bt);
        }
        setVisibility(View.VISIBLE);
    }

    /** 设置按钮文本 */
    public void setItemText(int[] resIdArray) {
        if (resIdArray == null || resIdArray.length <= 0) {
            return;
        }
        String[] txtArray = new String[resIdArray.length];
        for (int i = 0; i < resIdArray.length; i++) {
            txtArray[i] = mContext.getString(resIdArray[i]);
        }
        setItemText(txtArray);
    }

    /** 更新文本颜色 */
    private void updateTextColor() {
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            Button bt = (Button) getChildAt(i);
            if (mCurIdx == i) {
                bt.setTextColor(mSelColor);
            } else {
                bt.setTextColor(mNormColor);
            }
        }
    }

    /** 更新背景 */
    private void updateBackGround() {
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            Button bt = (Button) getChildAt(i);
            if (mCurIdx == i) {
                bt.setBackgroundDrawable(null);
            } else {
                if (i == 0) {
                    bt.setBackgroundResource(mLeftBgResId);
                } else if (i == count - 1) {
                    bt.setBackgroundResource(mRightResId);
                } else {
                    bt.setBackgroundResource(mMidBgResId);
                }
            }
        }
    }

    /** 获取选中的按钮 */
    public int getSel() {
        return mCurIdx;
    }

    /** 设置选中某个按钮 */
    public void setSel(int idx) {
        mCurIdx = idx;

        notifySel();
        updateTextColor();
        updateBackGround();
    }

    @Override
    public void onClick(View v) {
        int id = (Integer) v.getTag();
        if (mCurIdx != id) {
            setSel(id);
        }
    }

    /** 触发选中事件 */
    private void notifySel() {
        if (mOnSwitchListener != null) {
            mOnSwitchListener.onSwitch(mCurIdx);
        }
    }

    /** 设置监听器 */
    public void setOnSwitchListener(OnSwitchListener onSwitchListener) {
        mOnSwitchListener = onSwitchListener;
    }

    public interface OnSwitchListener {
        public void onSwitch(int switchIndex);
    }
}
